Ext.define('AmpleHarvest.objects.metrics.util.cMetricsUtils', 
{
	extend		: 'Ext.util.Observable',
	id			: 'ampleharvest-objects-metrics-util-cmetricsutils',
	alias		: 'widget.ampleharvest-objects-metrics-util-cmetricsutils',
//	targetURL	: 'http://192.168.20.132:8080/',
	//TODO: move this out to a config
	serverPath	: '/tpds_services/',

	getLink	: function(arr_root, arr_linkTree)
	{
		if(Ext.isString(arr_root))
		{
			arr_root = [arr_root];
		}
		
		// TODO: might not need the below check anymore
		if(Ext.isObject(arr_linkTree))
		{
			arr_linkTree = [arr_linkTree];
		}

		if(Ext.isArray(arr_linkTree))
		{
			for (var var_i in arr_root)
			{
   				for (var var_j in arr_linkTree)
   				{
   					if(arr_linkTree[var_j]['rel'] == arr_root[var_i])
   					{
   						return this.serverPath+arr_linkTree[var_j]['urn'];
   					}
   				}
			}
		}
		return false;
	},
	
	getPages : function(arr_pages)
	{
		if(Ext.isArray(arr_pages))
		{
			arr_pages = [arr_pages];
		}
		return(arr_pages);
	},
    // TODO : should no longer be needed
	stripServer : function(str_server)
	{
		if(Ext.isString(str_server))
		{
			var arr_string = str_server.split(this.targetURL);
			if(Ext.isArray(arr_string))
			{
				return('/tpds_services/'+arr_string[1]);
			}
		}
		return false
	},
	
	buildLink : function(str_server)
	{
		if(Ext.isString(str_server))
		{
			return this.serverPath+str_server;
		}
		return false
	},
	
	get_json : function(obj_extraParams, caller)
	{
		var eventName = 'loadData';
		if(obj_extraParams.eventOverride)
		{
			eventName = obj_extraParams.eventOverride;
		}
		Ext.Ajax.request(
		{
			url		: obj_extraParams.restPath,
			method	: 'GET',
			params	: obj_extraParams,
			timeout	: 100000,
			success	: function(response)
			{
				var jsonObject = Ext.decode(response.responseText);
				if(Ext.isObject(obj_extraParams.passthrough))
				{
					jsonObject = { 
							passthrough : obj_extraParams.passthrough,
							data		: jsonObject
					}
					
				}
				caller.fireEvent(eventName, jsonObject);
			},
			failure	: function(obj_Response)
			{
				console.log(obj_Response.responseText);
			}
   		});
    },

    parseTitle : function(str_title)
    {
	    try
	    {
	    	str_title = str_title.charAt(0).toUpperCase() + str_title.substr(1);
	    	arr_title = new Array();
	    	arr_title = str_title.match(/[A-Z][a-z]+/g);
	    	str_return = '';
	    	for (variable in arr_title)
	    	{
	    		str_return += arr_title[variable]+' ';
	    	}
	    	return(str_return);
	    }
	    catch(e)
	    {
	    	console.log(e);
	    }
    },
    
    findByNameValue : function(arr_objects, str_name, str_value)
    {
    	for (var x in arr_objects)
    	{
    		if(arr_objects[x][str_name] == str_value)
    		{
    			return arr_objects[x];
    		}
    	}
    	return false;
    },
    
    createList	: function(obj_item, obj_config)
	{
		var obj_passData = '';
		if(Ext.isObject(obj_item[obj_config.type]))
		{
			obj_passData = obj_item[obj_config.type].data;
		} else if(Ext.isArray(obj_item[obj_config.type]))
		{
			obj_passData = obj_item[obj_config.type];
		}
		
		var str_href_path = this.getLink(obj_config.type, obj_item['link-tree'].link);

		//str_href_path = this.stripServer(str_href_threads)
		if (str_href_path||Ext.isArray(obj_passData))
		{
			this.m_obj_threadsObject = Ext.create("AmpleHarvest.objects.metrics.sitesRollup.cList",
					{
						title			: obj_config.title,
						anchor			: obj_config.anchor,
						columns			: obj_config.columns,
						multiSelect		: obj_config.multiSelect,
					    obj_data		: obj_passData,
						str_directPath 	: str_href_path,
						AHcaller		: obj_config.caller,
						bool_showTbar	: obj_config.tbar,
						showDefaultTbar	: obj_config.showDefaultTbar,
						tbarOptional	: obj_config.tbarOptional,
						bool_detailLoad : obj_config.detail,
						bool_reportLoad : obj_config.report
					});
		}
	}
    
});